(function() {
  'use strict';

  var classCallCheck = function(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  };

  var createClass = function() {
    function defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    return function(Constructor, protoProps, staticProps) {
      if (protoProps) defineProperties(Constructor.prototype, protoProps);
      if (staticProps) defineProperties(Constructor, staticProps);
      return Constructor;
    };
  }();

  var defaults = {
    box: '#content',
    itemClass: '.list__tab_item',
    active: 'tab_item_active',
    index: 0
  };

  var Tab = function() {
    function Tab(id, params) {
      classCallCheck(this, Tab);
      this.id = typeof id === 'object' ? id : document.querySelector(id);
      if (!this.id) {
        return;
      }
      this.params = extend(true, defaults, params);
      this.box = document.querySelector(this.params.box);
      this.item = this.id.querySelectorAll(this.params.itemClass);
      this.active = this.params.active;
      this.index = this.params.index;
      this.old = this.index;
      this.length = this.item.length;

      function normalizeEventName(eventName) {
        if (eventName.indexOf('on') !== 0) {
          if (eventName[0] !== eventName[0].toUpperCase()) {
            eventName = 'on' + eventName[0].toUpperCase() + eventName.substring(1);
          } else {
            eventName = 'on' + eventName;
          }
        }
        return eventName;
      }
      this.emitterEventListeners = {};
      this.emit = function(eventName) {
        // Trigger callbacks
        if (this.params[eventName]) {
          this.params[eventName](arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
        }
        var i;
        // Trigger events
        if (this.emitterEventListeners[eventName]) {
          for (i = 0; i < this.emitterEventListeners[eventName].length; i++) {
            this.emitterEventListeners[eventName][i](arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
          }
        }
      };
      this.on = function(eventName, handler) {
        eventName = normalizeEventName(eventName);
        if (!this.emitterEventListeners[eventName]) this.emitterEventListeners[eventName] = [];
        this.emitterEventListeners[eventName].push(handler);
        return this;
      };

      this.init();
    }

    createClass(Tab, [{
      key: 'init',
      value: function init() {
        this.layout();
        this.switching();
      }
    }, {
      key: 'layout',
      value: function layout() {
        var _this = this;
        [].forEach.call(_this.item, function(ele, index, array) {
          if (ele.classList.contains(_this.active)) {
            ele.classList.remove(_this.active);
          }
        });
        _this.item[_this.index].classList.add(_this.active);
      }
    }, {
      key: 'switching',
      value: function switching() {
        var _this = this;
        [].forEach.call(this.item, function(ele, index, array) {
          ele.addEventListener('click', function(ev) {
            _this.index = index;
            _this.emit('onClick', _this, this);
            if (_this.old === index) {
              return;
            }
            _this.old = _this.index;
            [].forEach.call(_this.item, function(ele, index, array) {
              ele.classList.remove(_this.active);
            });
            this.classList.add(_this.active);
          }, false);
        });
      }
    }]);
    window.Tab = Tab;
  }();

  var extend = function extend(deep, target, options) {
    for (name in options) {
      var copy = options[name];
      if (deep && copy instanceof Array) {
        target[name] = extend(deep, [], copy);
      } else if (deep && copy instanceof Function) {
        target[name] = options[name];
      } else if (deep && copy instanceof Object) {
        target[name] = extend(deep, {}, copy);
      } else {
        target[name] = options[name];
      }
    }
    return target;
  };

}());
